<?php if(! defined('SYSPATH')) exit('No direct script access allowed');
/*
 +----------------------------------------------------------------------+
 | QuickPHP Framework Version 0.10                                      |
 +----------------------------------------------------------------------+
 | Copyright (c) 2010 QuickPHP.net All rights reserved.                 |
 +----------------------------------------------------------------------+
 | Licensed under the Apache License, Version 2.0 (the 'License');      |
 | you may not use this file except in compliance with the License.     |
 | You may obtain a copy of the License at                              |
 | http://www.apache.org/licenses/LICENSE-2.0                           |
 | Unless required by applicable law or agreed to in writing, software  |
 | distributed under the License is distributed on an 'AS IS' BASIS,    |
 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
 | implied. See the License for the specific language governing         |
 | permissions and limitations under the License.                       |
 +----------------------------------------------------------------------+
 | Author: BoPo <ibopo@126.com>                                         |
 +----------------------------------------------------------------------+
*/
/**
 * QuickPHP 核心异常处理类
 * @see http://php.net/manual/en/language.exceptions.php
 *
 * @category    QuickPHP
 * @package     Exception
 * @author      BoPo <ibopo@126.com>
 * @copyright   Copyright &copy; 2010 QuickPHP
 * @license     http://www.quickphp.net/license/
 * @version     $Id: Exception.php 7240 2011-01-22 14:27:59Z bopo $
 */
class QuickPHP_Exception extends Exception
{

    /**
     * QuickPHP自定义异常处理,将程序错误的错误信息转化为人类可读内容
     *
     * @param   string   错误信息
     * @param   array    错误变量
     * @param   integer  异常代码
     * @return  void
     */
    public function __construct($message, array $variables = NULL, $code = 0)
    {
        $variable = array();

        if(QuickPHP::$errors === FALSE)
            $message = 'errors_disabled';
        else
            if(is_array($variables))
                foreach($variables as $key => $val)
                    $variable["{{$key}}"] = $val;

        list($file, $path) = explode('.',$message);

        if(empty($path))
        {
            $file = 'quickphp';
            $path = $message;
        }

        $message = QuickPHP::message($file, $path, $message);
        $message = strtr($message, $variable);

        parent::__construct($message, $code);
    }

    /**
     * 获取以文本方式异常错误信息:
     *
     * Error [ Code ]: Message ~ File [ Line ]
     *
     * @param   object  Exception
     * @return  string
     */
    public static function text()
    {
        if(QuickPHP::$is_cli === TRUE)
            return debug_print_backtrace();
        return debug_backtrace();
    }

    /**
     * __toString 魔术方法 .
     *
     * echo $exception;
     *
     * @uses    QuickPHP::exception_text
     * @return  string
     */
    public function __toString()
    {
        return QuickPHP::exception_text($this);
    }
}